<?php

namespace Home\Controller;

use Think\Controller;

class PublicController extends Controller {

    /**
     * 支付结果返回
     */
    public function notify() {
        $apitype = I('get.apitype');

        $pay = new \Think\Pay($apitype, C('payment.' . $apitype));
        if (IS_POST && !empty($_POST)) {
            $notify = $_POST;
        } elseif (IS_GET && !empty($_GET)) {
            $notify = $_GET;
            unset($notify['method']);
            unset($notify['apitype']);
        } else {
            exit('Access Denied');
        }
        //验证
        if ($pay->verifyNotify($notify)) {
            //获取订单信息
            $info = $pay->getInfo();

            if ($info['status']) {
                $payinfo = M("Pay")->field(true)->where(array('out_trade_no' => $info['out_trade_no']))->find();
                
                if ($payinfo['status'] == 0) {
                    
                    $order = M('order')->where('pay_id='.$payinfo['pay_id'])->find();

                    $payMap['pay_id'] = $payinfo['pay_id'];       
                    $payData['status'] = 1;
                    $payData['update_time'] = time();
                    $payData['pay_source'] = 0;
                    $payData['callback'] = 'http://wx.congyicn.com/Wechat/Api/Pay/orderId='.$order['order_id'];

                    M('pay')->where($payMap)->save($payData);

                    $orderMap['order_id'] = $order['order_id'];

                    $orderData['status'] = 1;
                    $orderData['pay_time'] = time();

                    M('order')->where($orderMap)->save($orderData);
                    
                    $lawyer = M('lawyer')->alias('l')->join('left join l_user u on u.uid = l.uid')->where('l.lawyer_id='.$order['lawyer_id'])->field('l.*,u.mobile,u.truename')->find();
                    $user = M('user')->where('uid='.$order['uid'])->find();
                    $attr = M('lawyer_attr')->where('attr_id='.$order['domain'])->find();
                    //支付后通知用户
                    mms(C('mmskey'),$user['mobile'],"您已成功预约".$attr['attr_name']."领域的".$lawyer['truename']."律师，律师将在2小时内回电，请注意接听。您可在轻律师微信号“我的账户-客户-已预约咨询”中查看。");

                    //支付后通知律师
                    mms(C('mmskey'),$lawyer['mobile'], $user['truename']."已向您预约".$attr['attr_name']."领域的咨询并付款，请在轻律师微信号“我的账户-律师-已预约咨询”中查看问题简介，并在2小时内回电".$order['phonenum']."。完成咨询后请在微信号中提交电话时长。");

                    redirect('http://wx.congyicn.com/');
   
                }
            } else {
                $this->error("支付失败！");
            }
        } else {
            E("Access Denied");
        }
    }

    public function returnPlat() {
        $apitype = I('get.apitype');

        $pay = new \Think\Pay($apitype, C('payment.' . $apitype));
        if (IS_POST && !empty($_POST)) {
            $notify = $_POST;
        } elseif (IS_GET && !empty($_GET)) {
            $notify = $_GET;
            unset($notify['method']);
            unset($notify['apitype']);
        } else {
            exit('Access Denied');
        }
        //验证
        if ($pay->verifyNotify($notify)) {
            //获取订单信息
            $info = $pay->getInfo();

            $payinfo = M("Pay")->field(true)->where(array('out_trade_no' => $info['out_trade_no']))->find();
                
            $orderInfo = M("order")->where("pay_id=".$payinfo['pay_id'])->find();
            header('Location:/wechat/reservationLawer/reserveSuc/lawyerId/'.$orderInfo['lawyer_id']);
            
        } else {
            E("Access Denied");
        }
    }
    
}
